package com.mediacloud.app.newsmodule.fragment.activity

import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.jaeger.library.StatusBarUtil
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.newsmodule.R
import com.yoake.media.player.utils.XVideoBitRate
import com.yoake.media.player.utils.XVideoItem
import kotlinx.android.synthetic.main.activity_video_prew.*
import xyz.doikki.videoplayer.player.BaseVideoView
import xyz.doikki.videoplayer.player.VideoView
import xyz.doikki.videoplayer.util.PlayerUtils

class VideoPreActivity  : BaseBackActivity(){
    private var url: String? = null

    override fun showTitleBar(): Boolean {
        return false
    }

    override fun getLayoutResID(): Int {
        return R.layout.activity_video_prew
    }

    override fun getStatusBarColor(): Int {
        return Color.BLACK
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarUtil.setDarkMode(this)
        rootLayout.setBackgroundColor(Color.BLACK)
        url = intent.getStringExtra("URL")
        val video = XVideoItem("")
        val bitRate = XVideoBitRate("默认", url)
        video.bitRates.add(bitRate)
        player.autoPlay = true
        player.addVideoItem(video, true)
        Glide.with(this).setDefaultRequestOptions(
            RequestOptions()
                .frame(1000000)
                .centerCrop()
                .error(R.drawable.cctv7_loading_img) //可以忽略
                .placeholder(R.drawable.cctv7_loading_img) //可以忽略
        ).load(url).into(logoIv)
        player.setPoster(url,null)
        player.hideTitleLayoutWhenInNormalState()
        player.hideTitleMoreView()
        player.addOnStateChangeListener(object : BaseVideoView.OnStateChangeListener {
            override fun onPlayerStateChanged(playerState: Int) {

            }

            override fun onPlayStateChanged(playState: Int) {
                when (playState) {
                    VideoView.STATE_PLAYBACK_COMPLETED -> {
                        logoLayout.visibility = View.VISIBLE
                    }
                    VideoView.STATE_PREPARING->{
                        logoLayout.visibility = View.GONE
                    }
                    VideoView.STATE_PREPARED -> {

                        durationTv.text = PlayerUtils.stringForTime(player.duration.toInt())
                    }
                }
            }
        })
        backIv.setOnClickListener { finish() }
        playIv.setOnClickListener {
            player.start()
            logoLayout.visibility = View.GONE
        }
    }

    override fun onBackPressed() {
        if (player.isFullScreen) {
            player.onBackPressed()
        }
        super.onBackPressed()
    }
}